package com.example.wananrdroid;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import androidx.viewpager.widget.ViewPager;

import com.example.wananrdroid.adapter.XiangmuVpadapter;
import com.example.wananrdroid.bean.TreeBean;
import com.google.android.material.tabs.TabLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class XiangmuActivity extends AppCompatActivity {

    private TextView mXiangName;
    private ImageView mXiangIamge;
    private TabLayout mXiangTab;
    private ViewPager mXiangVp;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiangmu);
        initViews();
    }

    private void initViews() {
        mXiangName = findViewById(R.id.xiang_name);
        mXiangIamge = findViewById(R.id.xiang_iamge);
        mXiangTab = findViewById(R.id.xiang_tab);
        mXiangVp = findViewById(R.id.xiang_vp);
        intent = getIntent();
        String name = intent.getStringExtra("name");
        mXiangName.setText(name);
        mXiangIamge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initlist();
    }

    private void initlist() {
        List<TreeBean.DataBean.ChildrenBean> children = (List<TreeBean.DataBean.ChildrenBean>) intent.getSerializableExtra("children");
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> strings = new ArrayList<>();
        for (int j = 0; j < children.size(); j++) {
            String name = children.get(j).getName();
            strings.add(name);
            Bundle bundle = new Bundle();
            bundle.putInt("cid",children.get(j).getId());
            ListFragment listFragment = new ListFragment();
            listFragment.setArguments(bundle);
            fragments.add(listFragment);
        }
        XiangmuVpadapter xiangmuVpadapter = new XiangmuVpadapter(getSupportFragmentManager(), fragments, strings);
        mXiangVp.setAdapter(xiangmuVpadapter);
        mXiangTab.setupWithViewPager(mXiangVp);
    }
}
